package com.android.support.jhf.utils;

import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;

/**
 * 动画类
 * 
 * @author hongfeijia
 * 
 */
public class AnimationUtils {

	/**
	 * Constructor to use when building a TranslateAnimation from code
	 * 
	 * @param fromXType
	 *            Specifies how fromXValue should be interpreted. One of
	 *            Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
	 *            Animation.RELATIVE_TO_PARENT.
	 * @param fromXValue
	 *            Change in X coordinate to apply at the start of the animation.
	 *            This value can either be an absolute number if fromXType is
	 *            ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
	 * @param toXType
	 *            Specifies how toXValue should be interpreted. One of
	 *            Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
	 *            Animation.RELATIVE_TO_PARENT.
	 * @param toXValue
	 *            Change in X coordinate to apply at the end of the animation.
	 *            This value can either be an absolute number if toXType is
	 *            ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
	 * @param fromYType
	 *            Specifies how fromYValue should be interpreted. One of
	 *            Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
	 *            Animation.RELATIVE_TO_PARENT.
	 * @param fromYValue
	 *            Change in Y coordinate to apply at the start of the animation.
	 *            This value can either be an absolute number if fromYType is
	 *            ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
	 * @param toYType
	 *            Specifies how toYValue should be interpreted. One of
	 *            Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
	 *            Animation.RELATIVE_TO_PARENT.
	 * @param toYValue
	 *            Change in Y coordinate to apply at the end of the animation.
	 *            This value can either be an absolute number if toYType is
	 *            ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
	 * @param listener
	 */
	public static Animation translate(int fromXType, float fromXValue,
			int toXType, float toXValue, int fromYType, float fromYValue,
			int toYType, float toYValue, long durationMillis,
			AnimationListener listener) {

		TranslateAnimation animation = new TranslateAnimation(fromXType,
				fromXValue, toXType, toXValue, fromYType, fromYValue, toYType,
				toYValue);
		animation.setFillEnabled(true);
		animation.setInterpolator(new LinearInterpolator());
		animation.setFillAfter(true);
		animation.setDuration(durationMillis);
		animation.setAnimationListener(listener);

		return animation;
	}

	/**
	 * Constructor to use when building a ScaleAnimation from code
	 * 
	 * @param fromX
	 *            Horizontal scaling factor to apply at the start of the
	 *            animation
	 * @param toX
	 *            Horizontal scaling factor to apply at the end of the animation
	 * @param fromY
	 *            Vertical scaling factor to apply at the start of the animation
	 * @param toY
	 *            Vertical scaling factor to apply at the end of the animation
	 * @param pivotXType
	 *            Specifies how pivotXValue should be interpreted. One of
	 *            Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
	 *            Animation.RELATIVE_TO_PARENT.
	 * @param pivotXValue
	 *            The X coordinate of the point about which the object is being
	 *            scaled, specified as an absolute number where 0 is the left
	 *            edge. (This point remains fixed while the object changes
	 *            size.) This value can either be an absolute number if
	 *            pivotXType is ABSOLUTE, or a percentage (where 1.0 is 100%)
	 *            otherwise.
	 * @param pivotYType
	 *            Specifies how pivotYValue should be interpreted. One of
	 *            Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
	 *            Animation.RELATIVE_TO_PARENT.
	 * @param pivotYValue
	 *            The Y coordinate of the point about which the object is being
	 *            scaled, specified as an absolute number where 0 is the top
	 *            edge. (This point remains fixed while the object changes
	 *            size.) This value can either be an absolute number if
	 *            pivotYType is ABSOLUTE, or a percentage (where 1.0 is 100%)
	 *            otherwise.
	 */
	public static Animation scale(float fromX, float toX, float fromY,
			float toY, int pivotXType, float pivotXValue, int pivotYType,
			float pivotYValue, long durationMillis, AnimationListener listener) {

		ScaleAnimation animation = new ScaleAnimation(fromX, toX, fromY, toY,
				pivotXType, pivotXValue, pivotYType, pivotYValue);
		animation.setFillEnabled(true);
		animation.setInterpolator(new LinearInterpolator());
		animation.setFillAfter(true);
		animation.setDuration(durationMillis);
		animation.setAnimationListener(listener);

		return animation;
	}

	/**
	 * Constructor to use when building an AlphaAnimation from code
	 * 
	 * @param fromAlpha
	 *            Starting alpha value for the animation, where 1.0 means fully
	 *            opaque and 0.0 means fully transparent.
	 * @param toAlpha
	 *            Ending alpha value for the animation.
	 * @param durationMillis
	 * @param listener
	 */
	public static Animation alpha(float fromAlpha, float toAlpha,
			long durationMillis, AnimationListener listener) {

		AlphaAnimation animation = new AlphaAnimation(fromAlpha, toAlpha);
		animation.setFillEnabled(true);
		animation.setInterpolator(new LinearInterpolator());
		animation.setFillAfter(true);
		animation.setDuration(durationMillis);
		animation.setAnimationListener(listener);

		return animation;
	}

	/**
	 * 多个Animation一起播放
	 * 
	 * @param durationMillis
	 * @param listener
	 * @param animation
	 * @return
	 */
	public static Animation animationSet(long durationMillis,
			AnimationListener listener, Animation... animation) {

		AnimationSet animationSet = new AnimationSet(true);
		animationSet.setInterpolator(new LinearInterpolator());
		animationSet.setFillEnabled(true);
		animationSet.setFillAfter(true);
		animationSet.setDuration(durationMillis);
		animationSet.setAnimationListener(listener);
		for (Animation tempAnimation : animation) {
			animationSet.addAnimation(tempAnimation);
		}
		return animationSet;

	}

}
